#include "e_mod_main.h"
#ifdef HAVE_AMAROK

static int dcop_get_song(void *data, int type, void *event);
static int dcop_exit(void *data, int type, void *event);
static void _amarok_control_get_shuffle(Eloquence_Face *face);
static void _amarok_control_set_score_100(Eloquence_Face *face, 
		E_Menu *m, E_Menu_Item *mi);
static void _amarok_control_set_score_80(Eloquence_Face *face, 
		E_Menu *m, E_Menu_Item *mi);
static void _amarok_control_set_score_60(Eloquence_Face *face, 
		E_Menu *m, E_Menu_Item *mi);
static void _amarok_control_set_score_40(Eloquence_Face *face, 
		E_Menu *m, E_Menu_Item *mi);
static void _amarok_control_set_score_20(Eloquence_Face *face, 
		E_Menu *m, E_Menu_Item *mi);
static void _amarok_control_set_score_00(Eloquence_Face *face, 
		E_Menu *m, E_Menu_Item *mi);
static void _amarok_control_show_equalizer(Eloquence_Face *face, 
		E_Menu *m, E_Menu_Item *mi);

void amarok_init(Eloquence_Face *face){
  face->exe_handler_data = ecore_event_handler_add(
		           ECORE_EXE_EVENT_DATA,
			   dcop_get_song, 
			   face);
  face->exe_handler_exit = ecore_event_handler_add(
		           ECORE_EVENT_SIGNAL_EXIT, 
			   dcop_exit, 
			   face);
  face->exe0 = ecore_exe_pipe_run(
		  "/bin/sh",
		  ECORE_EXE_PIPE_READ |
		  ECORE_EXE_PIPE_WRITE |
		  ECORE_EXE_PIPE_READ_LINE_BUFFERED, 
		  NULL);
  _amarok_control_get_shuffle(face);
}

void amarok_free(Eloquence_Face *face){
  if (face->exe0){
	  ecore_exe_kill(face->exe0);
//	  ecore_exe_free(face->exe0);
  }
  if (face->exe_handler_data)
	  ecore_event_handler_del(face->exe_handler_data);
  if (face->exe_handler_exit)
	  ecore_event_handler_del(face->exe_handler_exit);
}

void amarok_init_menu(E_Menu *menu, Eloquence_Face *face){
  E_Menu *sub, *subm;
  E_Menu_Item *mi;
  
  mi = e_menu_item_new(menu);
  e_menu_item_label_set(mi, _("Play/Pause"));
  e_menu_item_callback_set(mi, amarok_control_play_pause, face);
  mi = e_menu_item_new(menu);
  e_menu_item_label_set(mi, _("Next Song"));
  e_menu_item_callback_set(mi, amarok_control_track_next, face);
  mi = e_menu_item_new(menu);
  e_menu_item_label_set(mi, _("Previous Song"));
  e_menu_item_callback_set(mi, amarok_control_track_prev, face);
  mi = e_menu_item_new(menu);
  e_menu_item_label_set(mi, _("Show Equalizer"));
  e_menu_item_callback_set(mi, _amarok_control_show_equalizer, face);

  subm = e_menu_new();
  mi = e_menu_item_new(subm);
  e_menu_item_label_set(mi, _("Execellent (100)"));
  e_menu_item_callback_set(mi, _amarok_control_set_score_100, face);
  mi = e_menu_item_new(subm);
  e_menu_item_label_set(mi, _("Great Song (80)"));
  e_menu_item_callback_set(mi, _amarok_control_set_score_80, face);
  mi = e_menu_item_new(subm);
  e_menu_item_label_set(mi, _("Pretty Good (60)"));
  e_menu_item_callback_set(mi, _amarok_control_set_score_60, face);
  mi = e_menu_item_new(subm);
  e_menu_item_label_set(mi, _("Could Be Better (40)"));
  e_menu_item_callback_set(mi, _amarok_control_set_score_40, face);
  mi = e_menu_item_new(subm);
  e_menu_item_label_set(mi, _("Quite Bad (20)"));
  e_menu_item_callback_set(mi, _amarok_control_set_score_20, face);
  mi = e_menu_item_new(subm);
  e_menu_item_label_set(mi, _("Awful (0)"));
  e_menu_item_callback_set(mi, _amarok_control_set_score_00, face);
  face->amarok_menu_score = subm;
  mi = e_menu_item_new(sub);
  e_menu_item_label_set(mi, _("Set Song's Score"));
  e_menu_item_submenu_set(mi, face->amarok_menu_score);
}

int amarok_update_song(Eloquence_Face *face){
  ecore_exe_send(face->exe0, "dcop amarok player nowPlaying\n", 30); 
}

static int dcop_get_song(void *data, int type, void *event){
  Eloquence_Face *face = data;
  Ecore_Exe_Event_Data *ev = event;

  if (ev->lines){
	  if (strcmp(ev->lines[0].line, "")){
		  if (!strcmp(ev->lines[0].line, "true")){
			  face->player_shuffle = 1;
			  return 1;
		  }
		  if (!strcmp(ev->lines[0].line, "false")){
			  face->player_shuffle = 0;
			  return 1;
		  }
		  face->song = ev->lines[0].line;
		  face->player_running = 1;
		  draw_text(face);
                  return 1;
	  }
  }
  face->song = "";
  face->player_running = 0;
  draw_text(face);
  return 1;
}

static int dcop_exit(void *data, int type, void *event){
  Ecore_Event_Signal_Exit *ev = event;
/*  
  printf("\nEloquence' forked Shell exited: %p\n\n", ev->exe);
*/  
  return 1;
}

void amarok_control_play_pause(Eloquence_Face *face){
  if (face->player_running == 1)
	  ecore_exe_send(face->exe0, "dcop amarok player pause\n", 25); 
  else
	  ecore_exe_send(face->exe0, "dcop amarok player play\n", 24); 
}

void amarok_control_track_next(Eloquence_Face *face){
  ecore_exe_send(face->exe0, "dcop amarok player next\n", 24); 
  amarok_update_song(face);
}

void amarok_control_track_prev(Eloquence_Face *face){
  ecore_exe_send(face->exe0, "dcop amarok player prev\n", 24); 
  amarok_update_song(face);
}

void amarok_control_vol_inc(Eloquence_Face *face){
  ecore_exe_send(face->exe0, "dcop amarok player volumeUp\n", 28); 
}

void amarok_control_vol_dec(Eloquence_Face *face){
  ecore_exe_send(face->exe0, "dcop amarok player volumeDown\n", 30); 
}

void amarok_control_shuffle(Eloquence_Face *face){
  _amarok_control_get_shuffle(face);
  if (face->player_shuffle == 1)
	  ecore_exe_send(face->exe0, "dcop amarok player enableRandomMode 0\n", 38); 
  else
	  ecore_exe_send(face->exe0, "dcop amarok player enableRandomMode 1\n", 38); 
}

void amarok_add_file_callback(Eloquence_Face *face, 
		Evas_Object *obj, const char *emission, const char *source){
  /* Empty now, for drag'n'drop of files to play */
}

static void _amarok_control_get_shuffle(Eloquence_Face *face){
  ecore_exe_send(face->exe0, "dcop amarok player randomModeStatus\n", 36); 
}

static void _amarok_control_set_score_100(Eloquence_Face *face,
		                E_Menu *m, E_Menu_Item *mi){
  ecore_exe_send(face->exe0, "dcop amarok player setScore 100\n", 32); 
}

static void _amarok_control_set_score_80(Eloquence_Face *face,
		                E_Menu *m, E_Menu_Item *mi){
  ecore_exe_send(face->exe0, "dcop amarok player setScore 80\n", 31); 
}

static void _amarok_control_set_score_60(Eloquence_Face *face,
		                E_Menu *m, E_Menu_Item *mi){
  ecore_exe_send(face->exe0, "dcop amarok player setScore 60\n", 31); 
}

static void _amarok_control_set_score_40(Eloquence_Face *face,
		                E_Menu *m, E_Menu_Item *mi){
  ecore_exe_send(face->exe0, "dcop amarok player setScore 40\n", 31); 
}

static void _amarok_control_set_score_20(Eloquence_Face *face,
		                E_Menu *m, E_Menu_Item *mi){
  ecore_exe_send(face->exe0, "dcop amarok player setScore 20\n", 31); 
}

static void _amarok_control_set_score_00(Eloquence_Face *face,
		                E_Menu *m, E_Menu_Item *mi){
  ecore_exe_send(face->exe0, "dcop amarok player setScore 00\n", 31); 
}

static void _amarok_control_show_equalizer(Eloquence_Face *face,
		                E_Menu *m, E_Menu_Item *mi){
  ecore_exe_send(face->exe0, "dcop amarok player configEqualizer\n", 35); 
}

#endif
